<template>
  <div class="consumer-detail w-full h-full">
    <div class="btns w-full">
      <a-button type="primary" @click="consumerOperate('上线')" :disabled="table.rowSelection.selectedRowKeys.length<1">上线</a-button>
      <a-button type="primary" @click="consumerOperate('下线')" :disabled="table.rowSelection.selectedRowKeys.length<1">下线</a-button>
      <a-button type="primary" @click="consumerOperate('重启')" :disabled="table.rowSelection.selectedRowKeys.length<1">重启</a-button>
    </div>
    <a-table
      style="height:calc(100% - 40px);overflow-y:auto;"
      bordered
      :columns="table.columns"
      :rowKey="record => record.id"
      :row-selection="table.rowSelection"
      :data-source="table.dataSource"
      :pagination="false"
    >
    </a-table>
  </div>
</template>

<script>
import orupService from '@/api/orupService'
import moment from 'moment'

const columns = [
  {
    title: 'objectId',
    align: 'center',
    dataIndex: 'objectId'
  },
  {
    title: 'ID',
    align: 'center',
    dataIndex: 'id'
  },
  {
    title: '数量',
    align: 'center',
    dataIndex: 'retryQueueNums',
    customRender: (text) => {
      return text || 0
    }
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
    customRender: function (text, row, index) {
      return moment(text).format('YYYY-MM-DD HH:mm:ss')
    }
  }
]

export default {
  name: 'ConsumerDetail',

  data () {
    return {
      dataObject: {},
      filter: {
        filterText: '',
        pageIndex: 1,
        pageSize: 10
      },
      table: {
        columns: columns,
        dataSource: [],
        rowSelection: {
          selectedRowKeys: [],
          selectedRow: {},
          onChange: (selectedRowKeys, selectedRows) => {
            this.table.rowSelection.selectedRowKeys = selectedRowKeys.length ? [selectedRowKeys.pop()] : []
            this.table.rowSelection.selectedRow = selectedRows.length ? selectedRows.pop() : {}
          }
        }
      }
    }
  },
  props: {
    row: {
      type: Object,
      default: () => ({})
    }
  },
  created () {
    this.dataObject = this._.cloneDeep(this.row)
    this.loadData()
  },

  mounted () {},

  methods: {
    moment,
    loadData () {
      const params = {
        pageIndex: this.filter.pageIndex - 1,
        pageSize: this.filter.pageSize,
        filterText: this.filter.filterText,
        status: 0,
        consumerGroupId: this.dataObject.id
      }
      orupService.consumerListQuery(params).then(res => {
        if (res && res.length > 0) {
          this.table.dataSource = res
        }
      })
    },
    consumerOperate (type) {
      const id = this.table.rowSelection.selectedRow.id
      this.$confirm({
        title: '提示',
        content: `确定${type}？`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          if (type === '上线') {
            orupService.consumerOnline(id).then(res => {
              this.$message.success('上线成功!')
            })
          } else if (type === '下线') {
            orupService.consumerOnline(id).then(res => {
              this.$message.success('下线成功!')
            })
          } else if (type === '重启') {
            orupService.consumerRestart(id).then(res => {
              this.$message.success('重启成功!')
            })
          }
          this.table.rowSelection.selectedRowKeys = []
          this.table.rowSelection.selectedRow = {}
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.consumer-detail {
  background: #fff;
  .btns {
    height: 40px;
  }
}

</style>
